package com.ruoyi.web.controller.xcx;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.ACustomeruser;
import com.ruoyi.system.service.IACustomeruserService;
import com.ruoyi.system.service.ICustomerBusinessService;
import com.ruoyi.system.utils.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 48824
 * 小程序登陆业务类
 */
@RestController
@RequestMapping("/xcx/CustomerUserController")
public class CustomerUserController extends BaseController {


    @Autowired
    private ICustomerBusinessService iCustomerBusinessService;


    @Autowired
    private ServerConfig serverConfig;


    /*小程序登陆*/
    @PostMapping("/miniLogin")
    public AjaxResult miniLogin(@RequestBody ACustomeruser aCustomeruser)throws Exception{
        return  AjaxResult.success(iCustomerBusinessService.loginUser(aCustomeruser));
    }

    /*根据code获取openId进行登录*/
    @GetMapping("/miniLogin/{code}")
    public AjaxResult miniLoginCode(@PathVariable("code") String code)throws Exception {
        return success(LoginUtils.loginByOpenId(iCustomerBusinessService.getOpenId(code)));
    }
    /*
     * 获取用户信息
     * */
    @GetMapping ("/getXcxUser")
    public AjaxResult getXcxUser()
    {
        return success(LoginUtils.getUser());
    }



    /* 修改用户信息*/
    @PostMapping ("/updateXcxUser")
    public AjaxResult updateXcxUser(@RequestBody ACustomeruser aUser) {
        int i = iCustomerBusinessService.updateACustomeruser(aUser);
        if (i<=0){
            return error("用户信息修改失败");
        }
        return success("用户信息修改成功");
    }

    /*上传接口*/
    @PostMapping ("/upload")
    public AjaxResult   upload (@RequestBody MultipartFile file){
        try {
            try
            {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                AjaxResult ajax = AjaxResult.success();
                ajax.put("url", url);
                ajax.put("fileName", fileName);
                ajax.put("newFileName", FileUtils.getName(fileName));
                ajax.put("originalFilename", file.getOriginalFilename());
                return ajax;
            }
            catch (Exception e)
            {
                return AjaxResult.error(e.getMessage());
            }

        }catch (Exception e){
            return AjaxResult.error("文件上传出现异常"+e);
        }

    }

}
